﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using WooCoo.Data;
using WooCoo.ObjectModel.ManagedProperty;

namespace WooCoo.ObjectModel
{
    /// <summary>
    /// 业务集合类的基类，业务集合的 DataAccess方法与Entity不同
    /// </summary>
    /// <typeparam name="T">队列中的对象类型</typeparam>
    [Serializable]
    public partial class EntityList<T> : ObservableCollection<T>, IEntityOrList, IEntityList where T : Entity, new()
    {
        public EntityList()
        {

        }

        #region Delete and Undelete child

        private List<T> _deletedList;
        /// <summary>
        /// 保留被删除的对象。
        /// </summary>
        private List<T> DeletedList
        {
            get
            {
                if (this._deletedList == null) { this._deletedList = new List<T>(); }

                return this._deletedList;
            }
        }

        /// <summary>
        /// 删除指定的子对象
        /// </summary>
        /// <param name="child"></param>
        private void DeleteChild(T child)
        {
            //如果是新的对象，则不需要加入到 DeletedList 列表中。
            if (!child.IsNew)
            {
                // mark the object as deleted
                child.DeleteChild();

                // and add it to the deleted collection for storage
                this.DeletedList.Add(child);
            }
        }

        #endregion

        #region Inser,Remove,Clear重写
        protected override void InsertItem(int index, T item)
        {
            (item as IEntityOrList).SetParent(this);
            base.InsertItem(index, item);
        }

        protected override void RemoveItem(int index)
        {
            T child = this[index];
            base.RemoveItem(index);
            this.DeleteChild(child);
        }

        protected override void ClearItems()
        {
            foreach (var item in this) { this.DeleteChild(item); }
            base.ClearItems();            
        }
        #endregion

        #region 父子对象处理IEntityLink
        [NonSerialized]
        private IEntityOrList _parent;
        /// <summary>
        /// 父对象
        /// </summary>
        public IEntityOrList Parent
        {
            get { return this._parent; }
        }

        public void SetParent(IEntityOrList parent)
        {
            this._parent = parent;
            //this.MarkAsChild();
        }

        public void DeleteChild()
        {
            this.ClearItems();
        }
        #endregion

        #region 数据操作
        [NonSerialized]
        private IRepository _repository;
        /// <summary>
        /// 数据仓库操作，如果对于服务模式，该方式在服务端被调用
        /// 默认的数据库配置名为“DataBase”
        /// </summary>
        /// <returns></returns>
        public virtual IRepository GetRepository(string configName = "DataBase")
        {
            if (_repository == null)
                _repository = RepositoryFactoryHost.Factory.Create(typeof(T));
            //new DataAccess(PropertyInfoRepository.Instance.GetPropertyInfoSet(typeof(T)));
            return _repository;
        }

        #endregion
    }
}
